package com.openstack.javasdk;

import com.alibaba.fastjson.JSONObject;
import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient.OSClientV3;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.common.Identifier;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.model.compute.Limits;
import org.openstack4j.model.compute.SimpleTenantUsage;
import org.openstack4j.model.identity.v3.Endpoint;
import org.openstack4j.model.identity.v3.Role;
import org.openstack4j.model.identity.v3.Service;
import org.openstack4j.model.identity.v3.User;
import org.openstack4j.openstack.OSFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import com.openstack.javasdk.Auth;

@RestController
@RequestMapping(value = "/nova")
public class NovaModule {


//    You are not authorized to perform the requested action: identity:list_users.
    @GetMapping("/flavors")
    public String all(){
        OSClientV3 os = Auth.getOS();
        List<? extends Flavor> flavors = os.compute().flavors().list();
        return JSONObject.toJSONString(flavors);
    }

    @GetMapping("/createFlavor")
    public String roleList(){
        OSClientV3 os = Auth.getOS();
        Flavor flavor = Builders.flavor()
                .name("Large2")
                .ram(4096)
                .vcpus(6)
                .disk(120)
                .rxtxFactor(1.2f)
                .build();
        flavor = os.compute().flavors().create(flavor);
        return JSONObject.toJSONString(flavor);
    }

    @GetMapping("/flavors/query")
    public String flavorQuery(@RequestParam(value = "flavorId") String flavorId){
        OSClientV3 os = Auth.getOS();
        Flavor flavor = os.compute().flavors().get(flavorId);
        return JSONObject.toJSONString(flavor);
    }

    @GetMapping("/flavors/delete")
    public String flavorDelete(@RequestParam(value = "flavorId") String flavorId){
        OSClientV3 os = Auth.getOS();
        ActionResponse delete = os.compute().flavors().delete(flavorId);
        return JSONObject.toJSONString(delete);
    }


    @GetMapping("/images")
    public String images(){
        OSClientV3 os = Auth.getOS();
        Map<String, String> md = os.compute().images().getMetaData("815efee2-20bb-449a-8a4e-6211c43a557d");
        return JSONObject.toJSONString(md);
    }

    @GetMapping("/quota")
    public String quota(){
        OSClientV3 os = Auth.getOS();
        Limits limits = os.compute().quotaSets().limits();
        return JSONObject.toJSONString(limits);
    }

    @GetMapping("/tenantUsages")
    public String endpointList(){
        OSClientV3 os = Auth.getOS();
        List<? extends SimpleTenantUsage> tenantUsages = os.compute().quotaSets().listTenantUsages();
        return JSONObject.toJSONString(tenantUsages);
    }
}
